unit UserUtils;

interface

uses
  Vcl.Forms, StrUtils, SysUtils, DBModule, System.Variants;

type
  TOperation = (opNew, opEdit);

function SQLBeforeClause(SQL: string; Clause: string): string;
function GenerateInvoiceNo(Prefix: string; InvoiceNoLength: integer; LastNo: integer): string;
function VarToInt(AVariant: variant; DefaultValue: integer = 0): integer;
//procedure RefreshCountryStateCityDB;

implementation

function SQLBeforeClause(SQL: string; Clause: string): string;
var
  ClausePos: integer;
begin
  ClausePos := Pos(UpperCase(Clause), UpperCase(SQL));
  if ClausePos > 0 then
    Result := LeftStr(PChar(SQL), ClausePos - 1)
  else
    Result := SQL;
end;

function GenerateInvoiceNo(Prefix: string; InvoiceNoLength: integer; LastNo: integer): string;
var
  InvoiceFormat: string;
begin
  InvoiceFormat := Format('%s%%.%dd', [Prefix, InvoiceNoLength]);
  GenerateInvoiceNo := Format(InvoiceFormat, [LastNo]);
end;

{
procedure RefreshCountryStateCityDB(var Sender: TObject);
begin
  with TForm(Sender) do
  begin
    with fbCountry do
    begin
      if not Active then
        Open;
      Last;
      First;
    end;
    with fbState do
    begin
      if not Active then
        Open;
      Last;
      First;
    end;
    with fbCity do
    begin
      if not Active then
        Open;
      Last;
      First;
    end;
  end;
end;
}

function VarToInt(AVariant: variant; DefaultValue: integer = 0): integer;
begin
  Result := DefaultValue;
  if VarIsNull(AVariant) then
    Result := 0
  else
//    if VarIsOrdinal(AVariant) then
      Result := StrToInt(VarToStr(AVariant));
end;

end.
